﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Diploma.Models
{
    public partial class CollaborationRoom : ISoftDeletable
    {
        public bool IsCompleted
        {
            get { return false; }
        }

        //public string Theme 
        //{
        //    get 
        //    {
        //        if (Student != null)
        //        {
        //            string proposedTheme = CollaborationProposes.Where(p=>p.DeletionDate == null).First().Theme;
        //            if (proposedTheme == null)
        //                return BaseTheme;
        //            else
        //                return proposedTheme;
        //        }
        //        else
        //            return BaseTheme;
        //    }
        //}

        //public string Description
        //{
        //    get
        //    {
        //        if (Student != null)
        //        {
        //            string proposedDescription = CollaborationProposes.Where(p=>p.DeletionDate == null).First().Description;
        //            if (proposedDescription == null)
        //                return BaseDescription;
        //            else
        //                return proposedDescription;
        //        }
        //        else
        //            return BaseDescription;
        //    }
        //}

        public Student Student
        {
            get
            {
                IEnumerable<CollaborationPropose> proposes = CollaborationProposes.Where(p => p.DeletionDate == null);
                if (proposes.Count() == 1 && proposes.First().IsApproved)
                    return proposes.First().Student;
                return null;
            }
        }

        #region ISoftDeletable Members

        public void MarkForDeletion()
        {
            Mark(true);
        }


        public void UnmarkFromDeletion()
        {
            Mark(false);
        }

        #endregion

        public bool AllowAccess(User user)
        {
            if (user == null)
                return false;
            if ((Student == user
                 || Lecturer == user
                 || Consultant == user) && user.DeletionDate == null)
                return true;
            return false;
        }

        protected void Mark(bool asDeleted)
        {
            DeletionDate = asDeleted ? (DateTime?) DateTime.Now : null;
            Messages.ToList().ForEach(delegate(Message mes)
                                          {
                                              if (asDeleted) mes.MarkForDeletion();
                                              else mes.UnmarkFromDeletion();
                                          });
            Tasks.ToList().ForEach(delegate(Task t)
                                       {
                                           if (asDeleted) t.MarkForDeletion();
                                           else t.UnmarkFromDeletion();
                                       });
        }
    }
}